const Ajax = (options) => {
	const app = getApp();
	let contentType = 'application/json'
	let request_url = app.globalData.data.request_url
	let merchantId = app.globalData.data.merchantId
	let header = {}
	if (options.url == 'login') {
		//登录单独处理
		header = {
			'content-type': 'application/x-www-form-urlencoded',
		}
	} else {
		if(options.url === 'updateUser' || options.url === 'updatePwd' || options.url === 'changePhoneNumber'){
			header = {
				'content-type': 'application/x-www-form-urlencoded',
				cookie: uni.getStorageSync("shiro")
			}
		}else {
			header = {
				'content-type': contentType,
				cookie: uni.getStorageSync("shiro")
			}
		}
	}
	return new Promise((resolve, reject) => {
		uni.request({
			withCredentials: true,
			url: request_url + options.url,
			method: options.method || 'get',
			data: options.data || {},
			header: options.header || header,
			success: (res) => {
				if (res.statusCode === 200) {
					/**
					 * 登录接口单独处理
					 */
					if (options.url === 'login') {
						resolve(res)
					} else {
						if (res.data.success) {
							resolve(res.data.data)
						} else {
							if (options.url !== 'updatePwd' && options.url !== 'changePhoneNumber'){
								if (res.data.code === 401 || res.data.code === -1) {
									//清空缓存
									uni.clearStorageSync();
									app.globalData.showToast('请登录');
									uni.reLaunch({
										url: '/pages/login/login'
									})
								}
							}else {
								app.globalData.showToast(res.data.msg);
							}
							reject(res.data)
						}
					}
				} else {
					app.globalData.showToast(res.data.msg);
					reject(res.data)
				}
			},
			fail: (err) => {
				console.log(err )
				reject(err)
			},
		})
	})
}

export default Ajax